/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kma.summer2012.dao;

import com.kma.summer2012.model.CrumbsUser;
import com.kma.summer2012.factory.HibernateUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

/**
 * @author Администратор
 */
public class CrumbsUserDAOImpl implements CrumbsUserDAO/*DAO<CrumbsUser>*/{


    public void add(CrumbsUser user) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }


    public void update(CrumbsUser user) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(user);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }


    public CrumbsUser getById(int user_id) {
        Session session = null;
        CrumbsUser user = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            user = (CrumbsUser) session.get(CrumbsUser.class, user_id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return user;
    }


    public Collection<CrumbsUser> getAll() {
        Session session = null;
        List users = new ArrayList<CrumbsUser>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            users = session.createCriteria(CrumbsUser.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return users;
    }


    public void delete(CrumbsUser user) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(user);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    public CrumbsUser searchByLoginPassword(String login,String password) {
        Session session = null;
        CrumbsUser user = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            Query q= session.createQuery("from CrumbsUser where login like :login AND password like :pass");
            q.setParameter( "login", login);
            q.setParameter("pass", password+"%");
            user=(CrumbsUser) q.uniqueResult();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return user;
    }

}
